package com.app.micaihu.view.army.activity

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.app.base.BaseMvpActivity
import com.app.micaihu.R
import com.app.micaihu.configure.Config
import com.app.micaihu.view.army.contract.ArmyMemberContract
import com.app.micaihu.view.army.fragment.UserFansListFragment
import com.app.micaihu.view.army.fragment.UserFollowListFragment
import com.app.micaihu.view.army.presenter.ArmyMemberPresenter
import com.app.micaihu.view.main.adapter.TabLayoutViewPageAdapter
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import kotlinx.android.synthetic.main.army_invite_member_list_activity.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView

/**
 * 军团成员邀请列表
 */
class ArmyInviteMemberListActivity : BaseMvpActivity<ArmyMemberContract.View, ArmyMemberPresenter>(), ArmyMemberContract.View {

    override fun getLayoutId() = R.layout.army_invite_member_list_activity

    var bundle: Bundle? = null
    override fun getBundle(bundle: Bundle) {
        this.bundle = bundle
        gid = bundle.getString(Config.Extra.id, "")
    }

    override fun getPresenter() = ArmyMemberPresenter(gid)
    private var fragments: MutableList<Fragment> = arrayListOf()
    private var titles = listOf("关注", "粉丝")
    private var gid = ""

    override fun setData() {
        BarUtils.addMarginTopEqualStatusBarHeight(rlTitle)
        initFragment()
    }

    override fun addListeners() {
        tvBack.setOnClickListener {
            finish()
        }

    }

    private fun initFragment() {
        val userFollowListFragment = UserFollowListFragment()
        userFollowListFragment.arguments = bundle
        val userFansListFragment = UserFansListFragment()
        userFansListFragment.arguments = bundle
        fragments.add(userFollowListFragment)
        fragments.add(userFansListFragment)
        val viewPageAdapter = TabLayoutViewPageAdapter(supportFragmentManager, fragments, titles)
        viewPager.adapter = viewPageAdapter
        val commonNavigator = CommonNavigator(thisContext)
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return fragments.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val simplePagerTitleView: SimplePagerTitleView = ColorTransitionPagerTitleView(context)
                simplePagerTitleView.text = titles[index]
                simplePagerTitleView.textSize = 16f
                simplePagerTitleView.normalColor = ColorUtils.getColor(R.color.color222)
                simplePagerTitleView.selectedColor = ColorUtils.getColor(R.color.color222)
                simplePagerTitleView.setOnClickListener { viewPager.currentItem = index }
                return simplePagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator {
                val indicator = LinePagerIndicator(context)
                indicator.lineHeight = UIUtil.dip2px(context, 2.0).toFloat()
                indicator.setColors(ColorUtils.getColor(R.color.app_theme))
                return indicator
            }

        }
        viewPager.offscreenPageLimit = fragments.size
        magicIndicator.navigator = commonNavigator
        ViewPagerHelper.bind(magicIndicator, viewPager)

    }
}